﻿using Ma.Bussiness.AttributeExtend.AttributeModels;
using Ma.Bussiness.ModelExtend;
using System.Reflection;

namespace Ma.Bussiness.AttributeExtend
{
    /// <summary>
    /// 特性调用示例
    /// </summary>
    public static class AttributeHelper
    {
        /// <summary>
        /// 获取Enum特性值
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetRemark(this Enum value)
        {
            Type type = value.GetType();
            var field = type.GetField(value.ToString());
            if (field.IsDefined(typeof(RemarkAttribute), true))
            {
                RemarkAttribute attribute = (RemarkAttribute)field.GetCustomAttribute(typeof(RemarkAttribute), true);
                return attribute.Remark;
            }
            else
            {
                return value.ToString();
            }
        }
        /// <summary>
        /// 数据校验
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static bool Validate<T>(this T t)
        {
            Type type = t.GetType();
            foreach (var prop in type.GetProperties())
            {
                if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
                {
                    object oValue = prop.GetValue(t);
                    foreach (AbstractValidateAttribute attribute in prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true))
                    {
                        if (!attribute.Validate(oValue))
                            return false;
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 获取属性中文名称
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        public static string GetColumnName(this PropertyInfo prop)
        {
            return Cache.GetColumnChineseName(prop);
        }

        /// <summary>
        /// 获取实体属性对应数据库的字段名称
        /// </summary>
        /// <param name="prop"></param>
        /// <returns></returns>
        public static string GetMappingColumnName(this PropertyInfo prop)
        {
            return Cache.GetColumnMappingName(prop);
        }

        /// <summary>
        /// 数据库名称不一致
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static string GetMappingTName<T>()
        {
            return CacheHelper<T>.MappingName;
        }


        public static bool Validate<T>(this T t, out List<string> errorList) where T : BaseModel
        {
            errorList = new List<string>();
            var properties = CachePropertyAttribute<T>.Property_Attribute_Cache.Keys;
            foreach (PropertyInfo prop in properties)
            {
                var propValue = prop.GetValue(t);
                var attributeArray = CachePropertyAttribute<T>.Property_Attribute_Cache[prop];
                foreach (AbstractValidateAttribute attribute in attributeArray)
                {
                    if (!attribute.Validate(propValue))
                    {
                        errorList.Add($"{prop.GetColumnName()} - 值：{propValue}  错误信息 ");
                    }
                }
            }
            return errorList.Count == 0;
        }
    }
}
